// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database();
// 云函数入口函数
exports.main = async (event, context) => {
    const wxContext = cloud.getWXContext()
    console.log(event);
    const {
        startMode,
        userName,
        userId,
        projectId,
        start,
        end,
        front = [],
        back = [],
        last,
        status,
        name,
        request,
        id
    } = event;
    if (id) {
        const origin = await db.collection('mission').doc(id).field({
            front: true
        }).get().then(res => res.data)
        if (origin.front && origin.front.length) {
            //清除前置任务后置索引
            const promiseList = [];
            for (const item of origin.front) {
                promiseList.push(new Promise((resolve) => {
                    db.collection('mission').doc(item).field({
                        back: true
                    }).get().then(res => {
                        const index = res.data.back.indexOf(id);
                        if (index) {
                            res.data.back.splice(index, 1);
                            db.collection('mission').doc(item).update({
                                data:{
                                    back:db.command.set(res.data.back)
                                }
                            }).then(resolve);
                        }
                        else {
                            resolve()
                        }
                    }).catch(resolve)
                }))
            }
            await promiseList;
        }
    }
    if (startMode === 0) {
        let res;
        if (!id) {
            res = await db.collection('mission').add({
                data: {
                    userId,
                    userName,
                    projectId,
                    start,
                    end,
                    front,
                    back,
                    last,
                    status,
                    init: true,
                    name,
                    createTime: db.serverDate()
                }
            })
            await cloud.callFunction({
                name: 'logMission',
                data: {
                    userId:wxContext.OPENID,
                    missionId: res._id,
                    description: '创建了任务'
                }
            })
        } else {
            res = await db.collection('mission').doc(id).update({
                data: {
                    start,
                    end,
                    front,
                    last,
                    status,
                    init: true,
                    name,
                    createTime: db.serverDate()
                }
            })
            await cloud.callFunction({
                name: 'logMission',
                data: {
                    userId:wxContext.OPENID,
                    missionId: id,
                    description: `更新了任务`
                }
            })
        }
        return res
    } else if (startMode === 1) {
        if (request) {
            const res = await db.collection('mission').add({
                data: {
                    ...request,
                    init: false,
                    createTime: db.serverDate()
                }
            })
            await cloud.callFunction({
                name: 'logMission',
                data: {
                    userId:wxContext.OPENID,
                    missionId: res._id,
                    description: `委托了任务`
                }
            })
            front.push(res._id)
        }
        let res;
        if (!id) {
            res = await db.collection('mission').add({
                data: {
                    userId,
                    userName,
                    projectId,
                    start,
                    end,
                    front,
                    back,
                    last,
                    status,
                    init: true,
                    createTime: db.serverDate(),
                    name
                }
            })
            await cloud.callFunction({
                name: 'logMission',
                data: {
                    userId:wxContext.OPENID,
                    missionId: res._id,
                    description: `创建了任务`
                }
            })
        } else {
            res = await db.collection('mission').doc(id).update({
                data: {
                    start,
                    end,
                    front,
                    last,
                    status,
                    init: true,
                    createTime: db.serverDate(),
                    name
                }
            })
            await cloud.callFunction({
                name: 'logMission',
                data: {
                    userId:wxContext.OPENID,
                    missionId: id,
                    description: `更新了任务`
                }
            })
        }

        return await db.collection('mission').doc(front[0]).update({
            data: {
                back: [res._id]
            }
        })

    }
}